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LmNydDANBgkqhkiG9w0BAQUF
AAOBgQCfQ89bxFApsb/isJr/aiEdLRLDLE5a+RLizrmCUi3nHX4adpaQedEkUjh5
u2ONgJd8IyAPkU0Wueru9G2Jysa9zCRo1kNbzipYvzwY4OA8Ys+WAi0oR1A04Se6
z5nRUP8pJcA2NhUzUnC+MY+f6H/nEQyNv4SgQhqAibAxWEEHXw==
-----END CERTIFICATE-----
EOF

# These are from 'openssl x509 -in google.pem -text -fingerprint -sha256'
google_sha256='f641c36cfef49bc071359ecf88eed9317b738b5989416ad401720c0a4e2e6352'
google_sha1='405062e5befde4af97e9382af16cc87c8fb7c4e2'
google_serial='2fdfbcf6ae91526d0f9aa3df40343e9a'
blacklist="$td/blacklist"
export OPENSSL_BLACKLIST_PATH="$blacklist"

die () {
	echo "$@"
	exit 1
}

verify () {
	openssl verify -CAfile "$td/thawte.pem" "$td/google.pem" > "$td/$1.out" 2> "$td/$1.err"
}

# First, ensure that the cert verifies with no changes.
verify good || die "failed to verify good signature"
echo "serial $google_serial" > "$blacklist"
verify serial && die "verified with blacklisted serial"
echo "sha256 $google_sha256" > "$blacklist"
verify sha256 && die "verified with blacklisted sha256"
echo "sha1 $google_sha1" > "$blacklist"
verify sha1 && die "verified with blacklisted sha1"
rm -rf "$td"
exit 0
